package com.service.calculate.hookArmCase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.service.bean.data.Data;
import com.service.bean.signal.GpsHookArmCaseSignal;
import com.service.other.Logger;

/**
 * 协议基础信息解析
 * @author lj
 *
 */
public class HookArmCaseBasalCalculator {
	
	/**
	 * 基础值计算
	 * @param data
	 * @param signal
	 */
	public void calculate(Data data, GpsHookArmCaseSignal signal) {
		signal.gpsTime = this.getGpsTime(data);
		if (null != data && null != data.latitude) {
			signal.lat = new BigDecimal(Long.parseLong(data.latitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		if (null != data && null != data.longitude) {
			signal.lng = new BigDecimal(Long.parseLong(data.longitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		signal.equipmentType = Integer.parseInt(data.vehicleType, 16);
	}
	
	/**
	 * GPS时间
	 */
	private Date getGpsTime(Data data) {
		StringBuilder builder = new StringBuilder();
		Date date = null;
		try {
			if (null == data || null == data.year || null == data.month
					|| null == data.day || null == data.hour || null == data.minute
					|| null == data.second) {
				return new Date();
			}
			builder.append(2000+Integer.parseInt(data.year, 16));
			builder.append("-");
			builder.append(Integer.parseInt(data.month, 16));
			builder.append("-");
			builder.append(Integer.parseInt(data.day, 16));
			builder.append(" ");
			builder.append(Integer.parseInt(data.hour, 16));
			builder.append(":");
			builder.append(Integer.parseInt(data.minute, 16));
			builder.append(":");
			builder.append(Integer.parseInt(data.second, 16));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(builder.toString());
		}catch(Exception e){
			Logger.err(e);
		}
		Date now =  new Date();
		
		if(date != null && date.getTime() - now.getTime() > 5*60*1000){
			return now;
		}else {
			return date;
		}
	}
	
}
